package aiball

import org.springframework.dao.DataIntegrityViolationException
import grails.plugins.springsecurity.Secured

class LeagueController {

	def springSecurityService
	
    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index() {
        redirect(action: "list", params: params)
    }

    def list() {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [leagueInstanceList: League.list(params), leagueInstanceTotal: League.count()]
    }

	@Secured(['ROLE_USER', 'ROLE_ADMIN'])
    def create() {
        [leagueInstance: new League(params)]
    }

    def save() {
        def leagueInstance = new League(params)
		leagueInstance.admin = ((UserAccount)springSecurityService.getCurrentUser())
        if (!leagueInstance.save(flush: true)) {
            render(view: "create", model: [leagueInstance: leagueInstance])
            return
        }

		flash.message = message(code: 'default.created.message', args: [message(code: 'league.label', default: 'League'), leagueInstance.id])
        redirect(action: "show", id: leagueInstance.id)
    }

    def show() {
        //def leagueInstance = League.get(params.id)
        //if (!leagueInstance) {
		//	flash.message = message(code: 'default.not.found.message', args: [message(code: 'league.label', default: 'League'), params.id])
        //    redirect(action: "list")
        //    return
        
		redirect(controller:"LeagueTeamPoints", view:"list", params: params)
		
		//}

        //[leagueInstance: leagueInstance]
    }
	@Secured(['ROLE_ADMIN'])
    def edit() {
        def leagueInstance = League.get(params.id)
        if (!leagueInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'league.label', default: 'League'), params.id])
            redirect(action: "list")
            return
        }

        [leagueInstance: leagueInstance]
    }
	@Secured(['ROLE_ADMIN'])
    def update() {
        def leagueInstance = League.get(params.id)
        if (!leagueInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'league.label', default: 'League'), params.id])
            redirect(action: "list")
            return
        }

        if (params.version) {
            def version = params.version.toLong()
            if (leagueInstance.version > version) {
                leagueInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
                          [message(code: 'league.label', default: 'League')] as Object[],
                          "Another user has updated this League while you were editing")
                render(view: "edit", model: [leagueInstance: leagueInstance])
                return
            }
        }

        leagueInstance.properties = params

        if (!leagueInstance.save(flush: true)) {
            render(view: "edit", model: [leagueInstance: leagueInstance])
            return
        }

		flash.message = message(code: 'default.updated.message', args: [message(code: 'league.label', default: 'League'), leagueInstance.id])
        redirect(action: "show", id: leagueInstance.id)
    }
	@Secured(['ROLE_ADMIN'])
    def delete() {
        def leagueInstance = League.get(params.id)
        if (!leagueInstance) {
			flash.message = message(code: 'default.not.found.message', args: [message(code: 'league.label', default: 'League'), params.id])
            redirect(action: "list")
            return
        }

        try {
            leagueInstance.delete(flush: true)
			flash.message = message(code: 'default.deleted.message', args: [message(code: 'league.label', default: 'League'), params.id])
            redirect(action: "list")
        }
        catch (DataIntegrityViolationException e) {
			flash.message = message(code: 'default.not.deleted.message', args: [message(code: 'league.label', default: 'League'), params.id])
            redirect(action: "show", id: params.id)
        }
    }

	def verwaltung(){
		[leagueInstanceList: League.list(params), leagueInstanceTotal: League.count(), aktuellEingeloggt : springSecurityService.getCurrentUser()]
	}
	
	def join(){
		
	}
}
